package com.atguigu.tingshu.account.mapper;

import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccount> {

    /**
     * 查询是否有可用金额
     * @param userId
     * @param amount
     * @return
     */
    @Select("select * from user_account where user_id = #{userId} and available_amount >= #{amount} for update ")
    UserAccount check(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    /**
     * 锁定金额
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account set lock_amount = lock_amount + #{amount}, available_amount = available_amount - #{amount} where user_id = #{userId}")
    int lock(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    /**
     * 真正扣减金额
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account set lock_amount = lock_amount - #{amount}, total_amount = total_amount - #{amount}, total_pay_amount = total_pay_amount + #{amount} where user_id = #{userId}")
    int minus(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    /**
     * 解锁金额
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account set lock_amount = lock_amount - #{amount}, available_amount = available_amount + #{amount} where user_id = #{userId}")
    int unlock(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    /**
     * 更新可用金额
     * @param userId
     * @param amount
     */
    @Update("update user_account set total_amount = total_amount + #{amount}, available_amount = available_amount + #{amount}, total_income_amount = total_income_amount + #{amount}  where user_id = #{userId}")
    void updateUserAccount(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    /**
     * 检查与扣减
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account set available_amount = available_amount - #{amount},\n" +
            "                        total_amount = total_amount - #{amount},\n" +
            "                        total_pay_amount = total_pay_amount + #{amount}\n" +
            "                    where user_id = #{userId} and available_amount >= #{amount}")
    int checkAndDeduct(@Param("userId") Long userId, @Param("amount") BigDecimal amount);
}
